package cn.tedu;

import java.sql.*;
import java.util.Scanner;

public class Demo07 {
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = sc.nextLine();
        System.out.println("请输入密码");
        String password = sc.nextLine();

        try (Connection  conn = DBUtils.getConn()){
//            Statement s = conn.createStatement();
//            String sql="select count(*) from user where username="+"'"+username+"'"+"and password="+ "'"+password+"'";
//            System.out.println(sql);
//            ResultSet resultSet = s.executeQuery(sql);

            String sql="select count(*) from user where username=+? and password=?";
            PreparedStatement ps = conn.prepareStatement(sql);

            ps.setString(1,username);
            ps.setString(2,password);

            ResultSet resultSet = ps.executeQuery();

            //让游标下移一位
            resultSet.next();
            int count=resultSet.getInt(1);
            if (count>0){
                System.out.println("登陆成功!");
            }else {
                System.out.println("用户名或密码错误");
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


    }
}
